// pages/autonym/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    valuename: '',
    valuenum: '',
    openid: '',
    userinfo: '',
    usercode: '',
    valuephone:'',
    superuserid:"",
    errormes:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //判断是否是有推荐人
    let usid=wx.getStorageSync('scene')
    if(usid=='undefined'){
      this.setData({
        superuserid:''
      })
    }else{
      this.setData({
        superuserid:usid
      })
    }
    console.log(this.data.superuserid);

    //获取userinfo
    this.data.userinfo = wx.getStorageSync('userinfo')
    console.log(this.data.userinfo);
    //获取usercode
    this.data.usercode = wx.getStorageSync('usercode')
    console.log(this.data.usercode);
  },
  onChangename() {
    const phone = this.data.valuename
    console.log(phone);
  },
  onChangesss() {
    console.log(this.data.valuephone);
    var phone= this.data.valuephone
    var k = this
    let meg = ''
    if (phone) {
      if (/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone)) {
        meg = '';
      } else {
        meg = '您输入的手机号码有误';
      }
    } 
    console.log(meg);
    k.setData({
      errormes: meg // 更改functions[0]中numbers的值
    })
  },
  onChangess(){
console.log(this.data.superuserid);
  },
  handletoque() {
    if (this.data.valuename == '' || this.data.valuenum == ""||this.data.errormes!="") {
      wx.showToast({
        title: '请正确填写信息',
        icon: 'error',
        duration: 1000,
        mask: true
      })
      return
    } else {
      wx.login({
        success: res => {
          var code = res.code;
          console.log(this.data.superuserid);
          if(this.data.superuserid){
            var aa=JSON.parse(this.data.superuserid)
          }
          wx.request({
            url: app.globalData.url+'/zkxj/wechat/register',
            data: {
              code: code,
              mobile:this.data.valuephone||null,
              identity: this.data.valuenum,
              realName: this.data.valuename,
              avatarUrl: this.data.userinfo.userInfo.avatarUrl,
              nickname: this.data.userinfo.userInfo.nickName,
              parentUserId:aa||''
              // wechatOpenId:this.data.openid
            },
            method: 'POST',
            header: {
              'content-type': 'application/json',
          'token':wx.getStorageSync('token')
            },
            success: function (res) {
              console.log(res);
              wx.setStorageSync('userId', res.data.userId)
              wx.setStorageSync('token', res.data.token)
              console.log(wx.getStorageSync('token'));
              if (res.data.code == 0) {
                wx.switchTab({
                  url: '/pages/mycenter/index',
                success: function (e) {
                  let page = getCurrentPages().pop();
                  if (page == undefined || page == null) return;
                      page.onLoad();
              }
            })
              } else if(res.data.code == 1002){
                wx.showToast({
                  title: '当前用户已实名',
                  icon: 'error',
                  duration: 1000,
                  mask: true
                })
                wx.switchTab({
                  url: '/pages/mycenter/index',
                })
              }
              else {
                wx.showToast({
                  title: '身份信息不匹配',
                  icon: 'error',
                  duration: 1000,
                  mask: true
                })
              }
            },
          })
        }
      })

    }


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})